package com.example.myapplication.activity;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.myapplication.MyApplication;
import com.example.myapplication.R;
import com.example.myapplication.entity.User;
import com.example.myapplication.utils.SharedPreferencesDao;

import java.util.List;

public class LoginActivity extends AppCompatActivity {

    private Button btnLogin;
    private Button btnRegister;
    private EditText etLoginUserName;
    private EditText etLoginPasswd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        bindView();
    }

    void bindView() {
        btnLogin = findViewById(R.id.btn_login);
        btnRegister = findViewById(R.id.btn_register);
        etLoginUserName = findViewById(R.id.login_username);
        etLoginPasswd = findViewById(R.id.login_passwd);
        btnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 登录
                String userName = etLoginUserName.getText().toString();
                String passWord = etLoginPasswd.getText().toString();
                Log.i("Login", "userName:"+userName);
                Log.i("Login", "passWord:"+passWord);

                if (userName == null || userName.equals("")) {
                    Toast res = Toast.makeText(getBaseContext(), "请输入用户名", Toast.LENGTH_SHORT);
                    res.show();
                    return;
                }
                if (passWord == null || passWord.equals("")) {
                    Toast res = Toast.makeText(getBaseContext(), "请输入密码", Toast.LENGTH_SHORT);
                    res.show();
                    return;
                }

                User user = SharedPreferencesDao.getUserInfo(getBaseContext(), userName);
                if (user.getUserName().equals(userName)) {
                    if (user.getPassword().equals(passWord)) {
                        // 登录成功
                        Toast res = Toast.makeText(getBaseContext(), "登录成功", Toast.LENGTH_SHORT);
                        res.show();
                        Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                        startActivity(intent);
                        return;
                    } else {
                        // 密码错误
                        Toast res = Toast.makeText(getBaseContext(), "密码错误", Toast.LENGTH_SHORT);
                        res.show();
                        return;
                    }
                } else {
                    // 用户名不存在
                    Toast res = Toast.makeText(getBaseContext(), "用户名不存在", Toast.LENGTH_SHORT);
                    res.show();
                    return;
                }
            }
        });

        btnRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 跳转注册界面
                Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
                startActivityForResult(intent, 1);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Log.i("onActivityResult", "requestCode: " + requestCode);
        Log.i("onActivityResult", "resultCodeCode: " + resultCode);
        if (resultCode == 1) {
            if (data == null) {
                Log.i("onActivityResult", "data is null");
                return;
            }
            final Bundle bundle = data.getBundleExtra("userInfo");
            Log.i("onActivityResult", bundle.getString("userName"));
            Log.i("onActivityResult", bundle.getString("password"));
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    etLoginUserName.setText(bundle.getString("userName"));
                    etLoginPasswd.setText(bundle.getString("password"));
                }
            });
        }
    }
}
